package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.constants.StatusConstants;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 45505
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

    @Override
    public PageResult<AgencyCertificationAudit> pageQuery(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {

        if (Objects.isNull(agencyCertificationAuditPageQueryReqDTO)) {
            throw new BadRequestException("分页信息不能为空");
        }
        Page<AgencyCertificationAudit> page = PageUtils
                .parsePageQuery(agencyCertificationAuditPageQueryReqDTO, AgencyCertificationAudit.class);
        LambdaQueryWrapper<AgencyCertificationAudit> eq = Wrappers.<AgencyCertificationAudit>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getName()),AgencyCertificationAudit::getName, agencyCertificationAuditPageQueryReqDTO.getName())
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getName()),AgencyCertificationAudit::getAuditStatus, agencyCertificationAuditPageQueryReqDTO.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(agencyCertificationAuditPageQueryReqDTO.getName()),AgencyCertificationAudit::getCertificationStatus, agencyCertificationAuditPageQueryReqDTO.getCertificationStatus());
        Page<AgencyCertificationAudit> agencyCertificationAuditPage = baseMapper.selectPage(page, eq);
        return PageUtils.toPage(agencyCertificationAuditPage,AgencyCertificationAudit.class);
    }

    @Override
    public void auditById(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        if (Objects.isNull(id) || Objects.isNull(certificationAuditReqDTO)){
            throw new BadRequestException("信息为空");
        }
        AgencyCertificationAudit certificationAudit = getById(id);
        certificationAudit.setAuditStatus(StatusConstants.ONE);
        certificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        if (certificationAuditReqDTO.getCertificationStatus().equals(StatusConstants.THREE)){
            certificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
            updateById(certificationAudit);
        }else {
            updateById(certificationAudit);
        }
    }

}
